<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/21
 * Time: 15:33
 */

namespace User\Controller;
use Common\Controller\FrontbaseController;
use User\Lib\CommentLib;
use User\Lib\PraiseLib;


class CallSampleController extends FrontbaseController{

	//此处拿文章表举例
	
	//评论列表
	public function index($from_uid = 0,$offset= 0,$limit= 0){
		$comment_list = CommentLib::comment_list('post',$from_uid,$offset,$limit);
		$this ->output($comment_list);
	}
	
	//针对对象(文章|商品|微博)评论
	public function comment_post($object_id=0,$to_uid=0,$content=''){
		//TODO 首先判断评论的对象是否存在
		
		// step2
		$res = CommentLib::comment_object('post',$object_id,$to_uid,$content);
		if(!$res[error]){
			//TODO 更新文章表的评论数量 和其他需要进行的业务操作
		}
		$this ->output($res);
	}
	
	//回复评论
	public function replay_comment($comment_id=0,$content=''){
		$res = CommentLib::reply_comment($comment_id,$content);
		$this->output($res);
	}
	
	//删除评论
	public function del_comment($comment_id = 0){
		$res = CommentLib::delete_comment($comment_id);
		//TODO  文章表的评论数减1 和 进行其他的业务操作
		$this->output($res);
	}
	
	//赞评论
	public function praise($object_id=0,$to_uid=0){
		$res = PraiseLib::praise('post',$object_id,$to_uid);
		if(!$res['error']){
			//TODO 根据 返回数据中的$res['type '] 来 把 相关对象表（如文章、评论表等） 的 点赞数量进行加 减操作 和其他业务操作
		}
		$this->output($res);
	}
	
	//当前用户是否 对文章或其他点赞
	public function is_praised($object_ids){
		
		if($object_ids && is_numeric($object_ids)){
			$object_ids  = strpos($object_ids,',') != false && strpos($object_ids,'[') === false  ? explode(',',$object_ids) : json_decode($object_ids);
		}
		$res = PraiseLib::is_praised('post',$object_ids,'id');
		//获取文章列表
		$post_list = M('Posts') -> limit(0,5)->select();
		//判断是否对文章进行点过赞
		if($res && $post_list ){
			$res['error'] = false;
			unset($res['error_msg']);
			foreach ($post_list as $k=> $v){
				$post_list[$k]['i_have_praised'] = isset($res['list'][$v['id']]) ? 1 : 0 ;
			};
		}
		$assign['list'] = $post_list;
		$this ->output($assign);
	}
}